package com.stone.mythread.juc;

import java.util.concurrent.CountDownLatch;

/**
 * 6个同学陆续离开教室后，班长才可以锁门
 */
public class ThreadCountDownLatch {

    public static void main(String[] args) throws InterruptedException {

        // 创建 CountDownLatch 对象，设置初始值
        CountDownLatch countDownLatch = new CountDownLatch(6);

        // 6 个同学陆续离开教室之后
        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.err.println(Thread.currentThread().getName() + " 号同学离开了教室。");
                // 每离开人，计数器减 1
                countDownLatch.countDown();
            }, String.valueOf(i)).start();
        }
        // 等待
        countDownLatch.await();

        System.err.println(Thread.currentThread().getName() + " 班长锁门走人了。");
    }
}
